package com.rui.study.demo.httpclient;

import com.rui.study.commons.util.StringUtil;
import com.rui.study.demo.httpclient.util.PrintUtil;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.util.Map;

/**
 * @program: study_20180903
 * @description:
 * @author: Yaowr
 * @create: 2018-10-29 10:23
 **/
public class HttpClient {

    public static void get(String url) {
        get(url, null);
    }

    public static void get(String url, Map<String, Object> headers) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpGet httpGet = new HttpGet(url);
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, Object> entry : headers.entrySet()) {
                    httpGet.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
                }
            }
            System.out.println("Executing request: " + httpGet.getRequestLine());
            CloseableHttpResponse response;
            try {
                response = httpclient.execute(httpGet);
                PrintUtil.print(response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void delete(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpDelete httpDelete = new HttpDelete(url);
            System.out.println("Executing request: " + httpDelete.getRequestLine());
            CloseableHttpResponse response;
            try {
                response = httpclient.execute(httpDelete);
                PrintUtil.print(response);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void putJson(String url, String json) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPut httpPut = new HttpPut(url);

            StringEntity entity = new StringEntity(json, ContentType.create("application/json", "UTF-8"));
            httpPut.setEntity(entity);

            System.out.println("Executing request: " + httpPut.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpPut);
            PrintUtil.print(response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void post(String url) {
        postJson(url, null);
    }

    public static void postJson(String url, String json) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(url);

            if (!StringUtil.isEmpty(json)) {
                StringEntity entity = new StringEntity(json, ContentType.create("application/json", "UTF-8"));
                httpPost.setEntity(entity);
            }

            System.out.println("Executing request: " + httpPost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httpPost);
            PrintUtil.print(response);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
